/* Process matching information from tags */

let tempTag = '<div id="box" value="13"     @click="click" show>123<a href="123.html"></a></div>'

function handleTag(_eleStr = '') {

    let tempTagName = '';
    let tempStrArr = _eleStr.split("");
    for (let i = 1; i < tempStrArr.length; i++) {
        if (tempStrArr[i] == '>' || tempStrArr[i] == ' ') {
            break;
        } else {
            tempTagName += tempStrArr[i]
        }
    }

    /* Is it a self-closing tag ? */
    let endStr = _eleStr.substring(_eleStr.length - (tempTagName.length + 3))
    let tempInnerHTML = null;
    if (endStr === `</${tempTagName}>`) { // 检测是不是闭合标签
        tempInnerHTML = _eleStr.substring(_eleStr.indexOf('>') + 1, _eleStr.length - endStr.length);
    }

    /* Get all Attributes */
    let attributesAreaStr = _eleStr.substring(`<${tempTagName}`.length, _eleStr.indexOf('>')).trim();
    let attributesArrEntry = [];
    let attributesArrOutput = [];
    let resultValue = null;
    if (attributesAreaStr.length !== 0) {
        attributesArrEntry = replaceDoubleSpace(attributesAreaStr).split(" ");
        attributesArrEntry.forEach(_item => {
            let tempKey = null;
            let tempValue = null;
            if (_item.indexOf("=\"") !== -1) {
                let tempArr = _item.split("=\"");
                tempKey = tempArr[0];
                tempValue = tempArr[1].slice(0, -1);
                tempKey === 'value' ? resultValue=tempValue:null;
            } else {
                tempKey = _item
            }
            attributesArrOutput.push({
                __attrKey: tempKey,
                __attrVal: tempValue
            })
        });
    }
    return {
        __value: resultValue,
        __innerHtml: tempInnerHTML,
        __tagName: tempTagName,
        __innerIncludeTag: tempInnerHTML ? tempInnerHTML.search(/[<].*[>]/) !== -1 : null,
        __attributes: attributesArrOutput
    }
}

console.log(handleTag(tempTag));

/* 把两个空格替换为一个空格 */

function replaceDoubleSpace(_str = '') {
    let resultStr = _str.replace('  ', ' ')
    return resultStr.search('  ') === -1 ? resultStr : replaceDoubleSpace(resultStr);
}